import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
  state: () => ({
    userInfo: getToken()
  }),
  mutations: {
    setUserInfo(state, result) {
      state.userInfo = result
      setToken(result)
    },
    removeUserInfo(state) {
      state.userInfo = {}
      removeToken()
    },
    SET_USERNAME(state, username) {
      state.userInfo.username = username
    },
    SET_USER_ROLE(state, role) {
      state.userInfo.userRole = role
    },
    SET_USER_AVATAR(state, avatar) {
      state.userInfo.userAvatar = avatar
    }
  },
  actions: {
    getUserInfo(context, data) {
      context.commit('setUserInfo', data)
    },
    logout(context) {
      context.commit('removeUserInfo')
    }
  },
  getters: {


    getUserId(state) {
      // debugger
      return state.userInfo.id
    },

    getUserAvatar(state) {
      return state.userInfo && state.userInfo.userAvatar || '';
    },
    getUsername(state) {
      return state.userInfo && state.userInfo.username || '';
    },
    getUserRole(state) {
      return state.userInfo && state.userInfo.userRole || '';
    }
  },
  namespaced: true,
}
export default user
